﻿var refreshGirdData; // 更新数据

var keyValue = request('keyValue');

var userdefault = {};
var alreadyformat = false;

var bootstrap = function ($, learun) {

    var pageWidth = 1024;

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            //初始化打印插件
            hiprint.init();
            pageWidth = ($('.lr-layout-grid').width());
            page.initGrid();
            page.bind();
            page.search();
        },
        bind: function () {

            // 查询
            $('#btn_Search').on('click', function () {
                page.search();
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            //打印
            $('#lr_print').on('click', function () {
                var formData = $('.lr-query-formcontent').lrGetFormData();
                var isRj = formData.isRj;

                var hiprintTemplate = null;
                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', '&code=pay', function (res) {
                    if (res.resp_code == 200) {
                        hiprintTemplate = new hiprint.PrintTemplate({
                            template: JSON.parse(res.data.templete),
                            paginationContainer: '.hiprint-printPagination'
                        });

                        //hiprintTemplate.design('#hiprint-printTemplate');
                        $.ajax({
                        url: config.base_server() + 'payvoucherbills/findById?id=' + keyValue + '&access_token=' + config.getToken().access_token,
                            type: "get",
                            dataType: "json",
                            async: false,
                            success: function (res) {
                                if (res.resp_code == 200) {
                                    var item = res.data;

                                    hiprintTemplate.resetPage();

                                    item.payAmt1 = chineseNumber(item.payAmt);
                                    item.payAmt2 = '￥' + item.payAmt;
                                    item.depBgtEcoName = item.depBgtEcoCode + " " + item.depBgtEcoName;
                                    item.expFuncName = item.expFuncCode + " " +item.expFuncName;


                                    var htmlObj = hiprintTemplate.getHtml(item);
                                    $('.prevViewDiv2').html(htmlObj);

                                    hiprintTemplate.print(item);
                                }
                            }
                        })
                    }
                });
                $('#A4_printByHtml').click(function () {
                    hiprintTemplate.printByHtml($('#myModal .modal-body .prevViewDiv'));
                })
            });

            //导出Excel
            $("#lr_export").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;
                learun.download({
                    url: config.base_server() + 'reports/cnrbb/bill',
                    param: params
                });
            });

        },
        initGrid: function () {

        },
        search: function (param) {
            param = param || {};

            var formData = $('.lr-query-formcontent').lrGetFormData();

            params = param;


            var hiprintTemplate = null;
            learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', '&code=pay', function (res) {
                if (res.resp_code == 200) {
                    hiprintTemplate = new hiprint.PrintTemplate({
                        template: JSON.parse(res.data.templete),
                        paginationContainer: '.hiprint-printPagination'
                    });

                    //hiprintTemplate.design('#hiprint-printTemplate');
                    $.ajax({
                        url: config.base_server() + 'payvoucherbills/findById?id=' + keyValue + '&access_token=' + config.getToken().access_token,
                        type: "get",
                        dataType: "json",
                        async: false,
                        success: function (res) {
                            if (res.resp_code == 200) {
                                var item = res.data;

                                item.payAmt1 = chineseNumber(item.payAmt);
                                item.payAmt2 = '￥' + item.payAmt;
                                item.depBgtEcoName = item.depBgtEcoCode + " " + item.depBgtEcoName;
                                item.expFuncName = item.expFuncCode + " " +item.expFuncName;

                                hiprintTemplate.resetPage();

                                var htmlObj = hiprintTemplate.getHtml(item);
                                $('.prevViewDiv2').html(htmlObj);
                            }
                        }
                    })
                }
            });
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    };

    page.init();
}



function chineseNumber(dValue) {
    var maxDec = 2;
    dValue = dValue.toString().replace(/,/g, "");
    dValue = dValue.replace(/^0+/, "");
    if (dValue == "") {
        return "零";
    } else if (isNaN(dValue)) {
        return "错误：金额不是合法的数值！";
    }
    var minus = "";
    var CN_SYMBOL = "";
    if (dValue.length > 1) {
        if (dValue.indexOf('-') == 0) {
            dValue = dValue.replace("-", "");
            minus = "负";
        }
        if (dValue.indexOf('+') == 0) {
            dValue = dValue.replace("+", "");
        }
    }
    var vInt = "";
    var vDec = "";
    var resAIW;
    var parts;
    var digits, radices, bigRadices, decimals;
    var zeroCount;
    var i, p, d;
    var quotient, modulus;
    var NoneDecLen = (typeof (maxDec) == "undefined" || maxDec == null || Number(maxDec) < 0 || Number(maxDec) > 5);
    parts = dValue.split('.');
    if (parts.length > 1) {
        vInt = parts[0];
        vDec = parts[1];
        if (NoneDecLen) {
            maxDec = vDec.length > 5 ? 5 : vDec.length;
        }
        var rDec = Number("0." + vDec);
        rDec *= Math.pow(10, maxDec);
        rDec = Math.round(Math.abs(rDec));
        rDec /= Math.pow(10, maxDec);
        var aIntDec = rDec.toString().split('.');
        if (Number(aIntDec[0]) == 1) {
            vInt = (Number(vInt) + 1).toString();
        }
        if (aIntDec.length > 1) {
            vDec = aIntDec[1];
        } else {
            vDec = "";
        }
    } else {
        vInt = dValue;
        vDec = "";
        if (NoneDecLen) {
            maxDec = 0;
        }
    }
    if (vInt.length > 44) {
        return "错误：数值过大！整数位长【" + vInt.length.toString() + "】超过了上限！";
    }
    digits = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖");
    radices = new Array("", "拾", "佰", "仟");
    bigRadices = new Array("", "万", "亿", "兆", "京", "垓", "杼", "穰", "沟", "涧", "正");
    decimals = new Array("角", "分", "厘", "毫", "丝");
    resAIW = "";
    if (Number(vInt) > 0) {
        zeroCount = 0;
        for (i = 0; i < vInt.length; i++) {
            p = vInt.length - i - 1;
            d = vInt.substr(i, 1);
            quotient = p / 4;
            modulus = p % 4;
            if (d == "0") {
                zeroCount++;
            } else {
                if (zeroCount > 0) {
                    resAIW += digits[0];
                }
                zeroCount = 0;
                resAIW += digits[Number(d)] + radices[modulus];
            }
            if (modulus == 0 && zeroCount < 4) {
                resAIW += bigRadices[quotient];
            }
        }
        resAIW;
    }
    for (i = 0; i < vDec.length; i++) {
        d = vDec.substr(i, 1);
        if (d != "0") {
            resAIW += digits[Number(d)] + decimals[i];
        }
    }
    if (resAIW == "") {
        resAIW = "零";
    }
    if (vDec == "") {
        resAIW;
    }
    console.log(CN_SYMBOL)
    console.log(minus)
    console.log(resAIW
    )

    resAIW = CN_SYMBOL + minus + resAIW;
    return resAIW;
}
